<?php

class systemConfigManager extends BaseModule {

	public function __construct() {
		parent::__construct();
	}

	public $configList = array();
	public $configTitleList = array();

	public function getSystemConfig() {
		$fileValue = file_get_contents(SYSTEMCONFIGPATH);
		$fileValueList = explode("\n", $fileValue);
		foreach ($fileValueList as $value) {
			if (str_replace(array("\n", "\r"), "", $value) == "") {
				continue;
			} elseif (strpos($value, "=") === false) {
				$titleList = explode("\t", $value);
				$configKey = substr($titleList[0], 1, -1);
				$this->configTitleList[$configKey] = substr($titleList[1], 1);
				continue;
			} else {
				$configOne = explode("\t", $value);
				$configOneCName = substr($configOne[1], 1);
				$configOneList = explode("=", $configOne[0]);
				$configOneEName = $configKey . "_" . substr($configOneList[0], 0, -1);
				$configOneValue = substr($configOneList[1], 1);
				if (strpos($configOneCName, "密码") != false) {
					$configOneType = "password";
				} else {
					$configOneType = "text";
				}
				$this->configList[$configKey][] = array($configOneCName, $configOneEName, $configOneValue, $configOneType);
			}
		}
	}

	public function formatData() {
		$titleList = array(
			"core" => array("核心配置"
				, array(
					"port" => "指令通信端口",
					"cliport" => "客户端端口",
					"downport" => "下载机端口",
					"dataport" => "数据传输端口",
					"count" => "重试次数",
					"ssl_key" => "SSL通信服务端证书",
					"ssl_cert" => "SSL通信客户端证书",
					"encrypt_file_dir" => "文件保存目录",
					"file_dir" => "文件路径",
					"tempory_dir" => "临时文件保存目录",
					"session_time" => "超时时间（秒）",
					"entype" => "加密类型",
				)),
			"db" => array("数据库配置"
				, array(
					"type" => "数据库类型",
					"host" => "数据库地址",
					"database" => "数据库名",
					"username" => "数据库用户名",
					"password" => "数据库访问密码",
				)),
			"downloader" => array("下载配置"
				, array(
					"ip" => "下载机IP",
					"addr" => "下载机MAC地址",
				)),
			"Plugin::Avidm" => array("服务器插件名称"
				, array(
					"class" => "插件类型",
					"interval" => "文件扫描时间（秒）",
					"server" => "FTP服务器IP地址",
					"port" => "FTP服务器端口",
					"username" => "FTP账号名",
					"password" => "FTP密码",
					"backup" => "备份文件夹名",
					"recorder_card" => "入库卡号",
				)),
			"limit" => array("上限配置"
				, array(
					"log_count" => "日志溢出条数",
					"user_file_size" => "用户空间配额限制（字节）",
					"user_file_time" => "文件有效时间（天）",
					"record_expire_time" => "光盘归还提醒时间（天）",
				)),
		);

		$configStr = "";
		foreach ($titleList as $key => $value) {
			$configStr .= "\n";
			$configStr .= "[{$key}]\t#{$value[0]}\n";
			foreach ($value[1] as $configKey => $configValue) {
				$configStr .= "{$configKey} = {$_POST[$key . "_" . $configKey]}\t#{$configValue}\n";
			}
		}
		return $configStr;
	}

	public function saveSystemConfig($configStr) {
		if (is_writable(SYSTEMCONFIGPATH)) {
			if (!$handle = fopen(SYSTEMCONFIGPATH, 'w')) {
				return "不能打开文件 " . SYSTEMCONFIGPATH;
			}
			if (fwrite($handle, $configStr) === FALSE) {
				return "不能写入到文件 " . SYSTEMCONFIGPATH;
			}
			fclose($handle);
			return "成功地写入到文件";
		} else {
			return "文件 " . SYSTEMCONFIGPATH . " 不可写";
		}
	}

}

?>